﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using ExperimentUpon.Util;
using ExperimentUpon.Service.Exception;
using ExperimentUpon.VO;
using NHibernate.Criterion;

namespace ExperimentUpon.Service.Impl
{
    public class ExperimentServiceImpl : IExperimentService
    {


        public List<ExperimentSubject> viewExperimentSubjects(int userId)
        {
            ISession session = NHibernateUtils.OpenSession();
            session.BeginTransaction();
            List<ExperimentSubject> expList = null;
            try
            {
                User user = session.Get<User>(userId);
                Role role = user.Role();
                if (Key.Role.StudentRoleId == role.Id)
                {
                    Student stu = session.Get<Student>(user.Id);
                    Speciality spe = stu.Clazz.Speciality;
                    expList = session.CreateCriteria<ExperimentSubject>()
                        .Add(Expression.Eq("Speciality", spe)).List<ExperimentSubject>();
                }
                else if(Key.Role.TeacherRoleId == role.Id)
                {
                    expList = session.CreateCriteria<ExperimentSubject>()
                        .List<ExperimentSubject>();
                }
                session.Transaction.Commit();
                return expList;
            }
            catch (System.Exception e)
            {
                session.Transaction.Rollback();
                throw new BaseException(e.Message, e);
            }
            finally
            {
                session.Close();
            }
        }

        public void publishExperimentSubject(int userId, int gradeId, int specialityId, ExperimentUpon.VO.ExperimentSubject subject)
        {
            throw new NotImplementedException();
        }

        public void submitAnswer(int userId, int subjectId, ExperimentUpon.VO.ExperimentAnswer answer)
        {
            throw new NotImplementedException();
        }

        public void remarkExperimentAnswer(int userId, int answerId, ExperimentUpon.VO.ExperimentRecord record)
        {
            throw new NotImplementedException();
        }

        public List<ExperimentUpon.VO.ExperimentRecord> viewExperimentRecordBySubjectId(int subjectId)
        {
            throw new NotImplementedException();
        }

        public ExperimentUpon.VO.ExperimentRecord viewExperimentRecordByAnswerId(int answerId)
        {
            throw new NotImplementedException();
        }

    }
}
